﻿#if UNITY_EDITOR

using System;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;

namespace AI.Graph.Editor {


    [Serializable]
    public class SerializedObject<T> where T : Object {

        [NonSerialized]
        private T m_target;

        [SerializeField]
        private int m_targetInstanceID;


        public T target {
            get {
                if (m_target == null) {
                    Object obj = EditorUtility.InstanceIDToObject (m_targetInstanceID);
                    if (obj.IsTypeOf<T> ()) {
                        m_target = obj as T;
                    }
                }

                return m_target;
            }
            set {
                if (value != null) {
                    m_targetInstanceID = value.GetInstanceID ();
                    m_target = value;
                }
                else {
                    m_targetInstanceID = -1;
                    m_target = null;
                }
            }
        }


        public int targetInstanceID {
            get { return m_targetInstanceID; }
        }

    }


}
#endif